package com.bjsxt.utils;


import com.alibaba.druid.sql.visitor.functions.Now;
import com.bjsxt.pojo.Admin;
import com.bjsxt.pojo.Survey;
import com.google.common.base.CaseFormat;
import com.google.common.collect.Maps;
import org.springframework.cglib.beans.BeanMap;

import java.util.Date;
import java.util.Map;

public class BeanMapUtils {


    public static <T> Map<String,Object> beanToMap(T bean){
        Map<String,Object> map = Maps.newHashMap();
        if (bean!=null){
            // BeanMap.create(bean) 将对象转换成map集合
          BeanMap beanMap =BeanMap.create(bean);
          for (Object key : beanMap.keySet()){
              map.put(key+"",beanMap.get(key));
          }
        }
        return map;
    }


    public static <T> Map<String,Object> beanToMapForUpdate(T bean){
        Map<String,Object> map = Maps.newHashMap();
        if(bean!=null){
            BeanMap beanMap = BeanMap.create(bean);
            for(Object key:beanMap.keySet()){
                map.put("update"+upperFirstLatter(key+""),beanMap.get(key));
            }
        }
        return map;
    }

    /**
     * 把传入字符串的首字母变为大写
     * @param letter
     * @return
     */
    public static String upperFirstLatter(String letter){
        char[] chars = letter.toCharArray();
        if (chars[0]>='a'&&chars[0]<='z'){
            chars[0]=(char)(chars[0]-32);
        }
        return new String(chars);
    }

    public static void main(String[] args) {
        Survey survey = new Survey();
        survey.setStartTime(new Date());
        System.out.println(beanToMapForUpdate(survey));

    }




}
